<?php
    require_once "weixin_info.php";
    require_once "config.php";
    $invite_code = empty($_GET['invite_code']) ? '' : $_GET['invite_code'];
    if(!empty($invite_code)){
        //判定当前用户是否为业务员
        $user_sql = $mysql->query('select * from dyspw_salesman where u_id = '.$_SESSION['Member_info'][0]['id']);
        $user = $user_sql->fetch_all(MYSQLI_ASSOC);
        if(!empty($user)){
            header("Location: https://5p8.fyh5p8.com/xiaochengxu/personalCenter.php");exit;
        }
        $sale_sql = $mysql->query('select * from dyspw_salesman where invite_code = '.$invite_code);
        $sale = $sale_sql->fetch_all(MYSQLI_ASSOC);
        $mem_sql = $mysql->query('select * from dyspw_member where id = '.$_SESSION['Member_info'][0]['id']);
        $mem = $mem_sql->fetch_all(MYSQLI_ASSOC);
        //接收邀请码，注册成为业务员状态为0
        $rand = rand(9999999,1000000);
        $invite_code = $rand . $mem[0]['id'];
        $time = time();
        //生成业务员邀请码二维码流程
        require_once "phpqrcode.php";
        define('UPLOAD_PATH', 'images/');
        define('UPLOAD_URL', '/xiaochengxu/images/qr_code/orders_hexiao/');
        //二维码流程
        $file_path =  'images/qr_code/salesman_code/';
        $qr_code_path = UPLOAD_PATH . 'qr_code/orders_hexiao/';//二维码保存的路径信息
        $sale_url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/salesman_up.php?invite_code=".$invite_code;//定义二维码包含的文字信息内容
        $shop_url = 'https://'.$_SERVER['HTTP_HOST']."/xiaochengxu/shop_edit.php?invite_code=".$invite_code;//定义二维码包含的文字信息内容
        $shop_url_file = $qr_code_path . $invite_code . "shop.png";
        $sale_url_file = $qr_code_path . $invite_code . "sale.png";
        $size = 5;
        if(!\QRcode::png($sale_url, $sale_url_file, QR_ECLEVEL_M, $size)){//png方法生成二维码并保存至指定文件夹
            $invite_img = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $invite_code . "sale.png";
        }
        if(!\QRcode::png($shop_url, $shop_url_file, QR_ECLEVEL_M, $size)){//png方法生成二维码并保存至指定文件夹
            $invite_shop = 'https://'.$_SERVER['HTTP_HOST'] . UPLOAD_URL . $invite_code . "shop.png";
        }
        $add_sale = $mysql->query("INSERT INTO dyspw_salesman (u_id, u_name, u_avatar,parent_id, invite_code, u_intro,invite_img,invite_shop, `time`) VALUES ('{$mem[0]['id']}', '{$mem[0]['username']}', '{$mem[0]['avatar']}',{$sale[0]['id']},$invite_code, '{$mem[0]['intro']}','$invite_img','$invite_shop',$time)");
        if($add_sale){
            header("Location: https://5p8.fyh5p8.com/xiaochengxu/member.php");
        }
    }
 ?>
